package com.elitetyc.customview.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.elitetyc.customview.R;

import java.util.Timer;
import java.util.TimerTask;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by elitetyc on 2019/6/11.
 * Describe:
 */
public class NoButtonDialog extends Dialog {
    private CircleImageView iv_dialog_type;
    private TextView tv_message;

    private Timer timer;
    private long time;
    private Activity activity;
    public NoButtonDialog(Context context, DialogEnum type, String message,long time) {
        super(context,R.style.MyDialog);
        this.time = time;
        setContentView(R.layout.dialog_no_button);


        iv_dialog_type = findViewById(R.id.iv_dialog_type);
        tv_message = findViewById(R.id.tv_message);


//        根据类型显示不同的图标
        switch (type){
            case DIALOG_OK:
                break;
            case DIALOG_TIMEING:
                break;
            case DIALOG_ERROR:
                break;
            case DIALOG_WRNING:
                break;
            default:
                break;
        }
        tv_message.setText(message);

        Window dialogWindow = getWindow();
        activity = (Activity) context;
        WindowManager m = activity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.95
        dialogWindow.setAttributes(p);
//        点击屏幕外和返回键都不关闭dialog
        setCancelable(false);
    }

    @Override
    public void show() {
        super.show();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismiss();
                    }
                });
            }
        },time);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        timer.cancel();
    }
}
